package com.edu.util.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author yanzhao
 * @since 2021/7/14
 */
public class FileReader {

    private static final Logger logger = LoggerFactory.getLogger(FileReader.class);

    /**
     * 根据路径加载数据
     *
     * @param
     * @return
     */
    public static String readClasspathFile2String(String classpath) {
        StringBuilder stringBuffer = new StringBuilder();
        ClassPathResource classPathResource = new ClassPathResource(classpath);
        try {
            InputStream stream = classPathResource.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                stringBuffer.append(line);
            }
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return String.valueOf(stringBuffer);
    }
}
